package com.example.boardroom.demo;

import static android.content.Context.NOTIFICATION_SERVICE;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.blankj.utilcode.util.ServiceUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.boardroom.R;
import com.example.boardroom.serial.table.other.SimpleService;

/***
 * 开机启动的广播
 */
public class BootCompleteReceiver extends BroadcastReceiver {
    private static final String TAG = "hello";
    private static final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Boot Complete 开机启动的广播接收者", Toast.LENGTH_LONG).show();
        Log.i(TAG, "onReceive: Boot Complete 开机启动的广播接收者");
        //开机的过程当中,启动 Activity的操作,判断当前启动的动作是开机启动的
        if (BOOT_ACTION.equals(intent.getAction())) {
            //开启Activity
            openActivity(context);
            //开启Service
            openService(context);
            ToastUtils.showLong("开机广播");
        }
    }

    /***
     * 启动Service的方法
     *
     * @param context
     */
    public void openService(Context context) {
        Intent newIntent = new Intent(context,HelloService.class);
        //判断当前编译的版本是否高于等于 Android8.0 或 26 以上的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(newIntent);
        } else {
            context.startService(newIntent);
        }
        Log.i(TAG, "openService: 启动Service");


//        if (!ServiceUtils.isServiceRunning(SimpleService.class)) {
//            ServiceUtils.startService(SimpleService.class);
//        }
    }

    /***
     * 启动Activity的方法
     *
     * @param context
     */
    public void openActivity(Context context) {
        //判断当前编译的版本是否高于等于 Android8.0 或 26 以上的版本
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            startActivityVersionHeight(context);
        } else {
            startActivityVersionLower(context);
        }
        Log.i(TAG, "openActivity: 启动Activity");
    }

    /***
     * 低版本的实现
     * @param context
     */
    public void startActivityVersionLower(Context context) {
        Intent myIntent = new Intent(context, MainActivity.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);
    }

    /***
     * 高版本的实现
     * @param context
     */
    public void startActivityVersionHeight(Context context) {

        Intent intent1 = new Intent(context, MainActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
                intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        String channelID = "my_channel_ID";
        String channelNAME = "my_channel_NAME";
        int level = NotificationManager.IMPORTANCE_HIGH;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager)
                    context.getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
            manager.createNotificationChannel(channel);
        }
        NotificationCompat.Builder notification =
                new NotificationCompat.Builder(context, channelID)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("Start Activity")
                        .setContentText("click me")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setAutoCancel(true)
                        .setFullScreenIntent(fullScreenPendingIntent, true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(100, notification.build());
    }
}
